package com.zzy.threadtest;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.*;

public class CurrentPoolStart {
    int corePoolSize = 5;//核心线程数
    int maxPoolSize = 100;//最大线程数
    long keepAliveTime = 120L;//线程最大活动时间
    BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1000);
    ThreadFactory threadFactory = Executors.defaultThreadFactory();
    RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

    ExecutorService executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler);

    public static void start(int threadCount) {
        CurrentPoolStart currentPoolStart = new CurrentPoolStart();
        currentPoolStart.doStart(threadCount);
    }

    public void doStart(int threadCount) {
        try {
            for (int i = 1; i < threadCount; i++) {
                int index = i;
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("线程dostart---->start---->" + index );
                        System.out.println(Thread.currentThread().getId() + "-->" + Thread.currentThread().getName());
                        System.out.println("线程dostart---->end---->" + index );
                    }
                });
            }


            ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
            int activeNum = threadGroup.activeCount();
            System.out.println("活动的线程个数：" + activeNum);
            Thread[] listThread = new Thread[activeNum];
            threadGroup.enumerate(listThread);

            System.out.println("当前活动的线程为：");
            for (Thread t : listThread) {
                System.out.println(t.getId() + "-->" + t.getName());
            }
            System.out.println("==========================================");
            //获取线程信息

            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            threadMXBean.setThreadContentionMonitoringEnabled(true);

            for (int i = 1; i < 10; i++) {//循环检测次数
                Thread.sleep(5000);

               long [] thredIds =  threadMXBean.getAllThreadIds();
               for(long id : thredIds){
                   ThreadInfo info = threadMXBean.getThreadInfo(id);
                   if(info==null){
                      continue;
                   }
                   String threadName = info.getThreadName();
                   long blockTime = info.getBlockedTime();
                   long waitTime = info.getWaitedTime();
                   long threadCpuTime = threadMXBean.getThreadCpuTime(id);
                   System.out.println("threadName-->"+ threadName
                           +"\t blockTime-->"+ blockTime +"\t waitTime-->"+ waitTime +"\t threadCpuTime-->"+ threadCpuTime);
               }
                System.out.println("----------------------------------------------");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        //executor.shutdown();//关闭线程池
    }


    public static void main(String[] args) {
        CurrentPoolStart.start(5);
    }

}
